package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.service.HosUserService;
import io.renren.modules.hos.vo.CommentVo;
import io.renren.modules.hos.vo.HosUserVo;
import io.renren.modules.hos.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.CommentDao;
import io.renren.modules.hos.entity.CommentEntity;
import io.renren.modules.hos.service.CommentService;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {
    @Autowired
    private HosUserService hosUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *  评论
     * @param commentParam
     * @return
     */
    @Override
    public boolean comment(CommentParam commentParam) {
        Long hosUserId = ShiroUtils.getHosUserId();
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setUserId(hosUserId);
        commentEntity.setMedicineId(commentParam.getMedicineId());
        commentEntity.setContent(commentParam.getContent());
        commentEntity.setCommentDate(new Date());
        Long parentId = commentParam.getParentId();
        if (parentId == null || parentId == 0){
            commentEntity.setLevel(1);
        } else {
            commentEntity.setLevel(2);
        }
        commentEntity.setParentId(parentId == null ? 0 : parentId);
        commentEntity.setToUserId(commentParam.getToUserId());
        baseMapper.insert(commentEntity);
        return true;
    }

    /**
     * 查询药品评论信息
     */
    @Override
    public List<CommentVo> selectCommentsByMedicineId(Long medicineId) {
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommentEntity::getMedicineId, medicineId);
        queryWrapper.eq(CommentEntity::getLevel, 1);
        queryWrapper.orderByDesc(CommentEntity::getCommentDate);
        List<CommentEntity> commentEntityList = baseMapper.selectList(queryWrapper);
        return copyList(commentEntityList);
    }

    public CommentVo copy(CommentEntity commentEntity){
        CommentVo commentVo = new CommentVo();
        Long id = commentEntity.getId();
        BeanUtils.copyProperties(commentEntity, commentVo);
        commentVo.setId(id);
        Long userId = commentEntity.getUserId();

        // 评论用户信息
        HosUserVo hosUserVo = hosUserService.selectUserById(userId);
        commentVo.setUser(hosUserVo);

        //子评论
        Integer level = commentEntity.getLevel();
        if (level == 1){
            List<CommentVo> commentVoList = selectCommentsByParentId(id);
            commentVo.setChildren(commentVoList);
        }

        //to_user给谁评论
        if (level > 1){
            Long toUserId = commentEntity.getToUserId();
            HosUserVo toUserVo = hosUserService.selectUserById(toUserId);
            commentVo.setToUser(toUserVo);
            List<CommentVo> commentVoList = selectCommentsByParentId(id);
            commentVo.setChildren(commentVoList);
        }
        return commentVo;
    }

    public List<CommentVo> copyList(List<CommentEntity> commentEntityList){
        List<CommentVo> commentVoList = new ArrayList<>();
        for (CommentEntity commentEntity : commentEntityList) {
            commentVoList.add(copy(commentEntity));
        }
        return commentVoList;
    }

    /**
     * 根据父评论id查询父评论信息
     * @param id
     * @return
     */
    private List<CommentVo> selectCommentsByParentId(Long id){
        LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommentEntity::getParentId, id);
        queryWrapper.eq(CommentEntity::getLevel, 2);
        return copyList(baseMapper.selectList(queryWrapper));
    }

}